<template>
	<view class="page">
		<video id="myVideo"
	    src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"
	    enable-danmu danmu-btn controls="false" autoplay='true' @timeupdate="videoUpdate" objectFit="fill"></video>
	  <view class='process-container'>
	    <image :src='[playStates ? "../../static/video.jpg" : "../../static/video.jpg"]' class='video-controls-icon' @tap='videoOpreation'></image>
	    <view class='slider-container'>
	      <slider @change="sliderChange" @changing="sliderChanging" step="1" :value="sliderValue" backgroundColor="#A8A8A8" activeColor="#FFEE83" block-color="#FFEE83" />
	    </view>
	  </view>
	</view>
</template>

<script lang="ts">
import Vue from 'vue';
import {
  mapState,
  mapGetters
} from 'vuex';

export default Vue.extend({
  data() {
    return {
      sliderValue: 0, //控制进度条slider的值，
      updateState: false, //防止视频播放过程中导致的拖拽失效
      playStates: true //控制播放 & 暂停按钮的显示
    }
  },
  onLoad() {

  },
  onReady() {
    this.videoContext = uni.createVideoContext('myVideo');
    this.updateState = true
  },
  computed: {

  },
  methods: {
    // videoUpdate在播放进度变化时触发，触发频率 250ms 一次。event.detail = {currentTime, duration}，currentTime表示当前时间，duration表示总时长，都以秒为单位。
    videoUpdate(e) {
      if (this.updateState) { //判断拖拽完成后才触发更新，避免拖拽失效
        let sliderValue = e.detail.currentTime / e.detail.duration * 100;
				this.sliderValue = sliderValue
				this.duration = e.detail.duration
      }
    },
    // 进度条可拖拽并指定视频跳转到相应的位置
    sliderChanging(e) {
		    this.updateState = false
    },
    sliderChange(e) {
      if (this.duration) {
        this.videoContext.seek(e.detail.value / 100 * this.duration); //完成拖动后，计算对应时间并跳转到指定位置
				this.sliderValue = e.detail.value
				this.updateState = true
      }
    },
    // 暂停/播放视频
    videoOpreation() {
      this.playStates ? this.videoContext.pause() : this.videoContext.play();
			this.playStates = !this.playStates
    }
  }
});
</script>

<style>
.content {
  text-align: center;
  height: 400upx;
}

.logo {
  height: 200upx;
  width: 200upx;
  margin-top: 200upx;
}

.title {
  font-size: 36upx;
  color: #8f8f94;
}
</style>
